package com.suda.es.elasticsearch.dao;

import com.suda.es.entity.Emp;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

/**
 * 自定义EmpRepository接口,需要继承ES提供的ElasticsearchRepository接口
 * ElasticsearchRepository接口中默认提供了一些用于操作ES的方法,只要继承此接口,就会自动创建对象并可以在项目中引入
 *      需要传入两个泛型
 *          第一个泛型为当前操作的对象
 *          第二个泛型为主键的类型,即对象中 @Id 注解用在字段的类型
 */
public interface EmpRepository extends ElasticsearchRepository<Emp,String> {

    //ElasticsearchRepository同时支持自定义扩展方法
    /*
        | 关键字                 | 示例                                               | 执行的查询语句,问号的个数等于传入参数的个数
        | --------------------- | ------------------------------------------------- | ------------------------------------------------------------
        | And                   | findByNameAndPrice                              | {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}
        | Or                    | findByNameOrPrice                               | {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}
        | Is                    | findByName                                      | {"bool" : {"must" : {"field" : {"name" : "?"}}}}
        | Not                   | findByNameNot                                   | {"bool" : {"must_not" : {"field" : {"name" : "?"}}}}
        | Between               | findByPriceBetween                              | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
        | LessThanEqual(小于等于) | findByPriceLessThan                             | {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
        | GreaterThanEqual(大于等于)| findByPriceGreaterThan                          | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}
        | Before                | findByPriceBefore                               | {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
        | After                 | findByPriceAfter                                | {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}
        | Like                  | findByNameLike                                  | {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}
        | StartingWith          | findByNameStartingWith                          | {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}
        | EndingWith            | findByNameEndingWith                            | {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}}
        | Contains/Containing   | findByNameContaining                            | {"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}}`
        | In                    | findByNameIn`<br>`(Collection<String>names)     | {"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}}
        | NotIn                 | findByNameNotIn`<br/>`(Collection<String>names) | {"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}}
        | True(某个字段的值是否为true)| findByAvailableTrue                             | {"bool" : {"must" : {"field" : {"available" : true}}}}
        | False(某个字段的值是否为false)| findByAvailableFalse                            | {"bool" : {"must" : {"field" : {"available" : false}}}}
        | OrderBy               | findByAvailable`<br>`TrueOrderByNameDesc        | {"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}}
     */

    //根据姓名查询
    List<Emp> findByName(String name);

    //根据年龄查询
    List<Emp> findByAge(Integer age);

    //根据姓名和地址和...查询
    List<Emp> findByNameAndAddress(String name, String address);

    //根据姓名或年龄或...查询
    List<Emp> findByNameOrAge(String name, Integer age);

    //查询年龄大于等于某个值的数据
    List<Emp> findByAgeGreaterThanEqual(Integer age);
}
